<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>tmpfs 是什么 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>tmpfs 是什么</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们df 的时候会看到一行 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># df</span></div><div class="line">Filesystem                   1K-blocks    Used Available Use% Mounted on</div><div class="line">/dev/mapper/VolGroup-lv_root  18102140 1429428  15753160   9% /</div><div class="line">tmpfs                           146844       0    146844   0% /dev/shm</div><div class="line">/dev/sda1                       495844   31636    438608   7% /boot</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它的大小为 146844，这个数值其实正好是内存大小的一半：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># free</span></div><div class="line">             total       used       free     shared    buffers     cached</div><div class="line">Mem:       293692     203528      90164          0      15880     125184</div><div class="line">-/+ buffers/cache:      62464     231228</div><div class="line">Swap:      2064376          0    2064376</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个tmpfs到底是什么呢? 其实它是一个临时文件系统，驻留于内存中，使用它可以提高文件访问速度，并能保证重启时会自动清除这些文件。只不过驻留在这里的文件是容易丢失的，因为内存数据是不会像硬盘中的数据那样可以永久存在。知道了tmpfs的这个特性后，我们就可以把一些对读写性能要求较高，但是数据又可以丢失的这样的数据就可以保存在/dev/shm中，你也可以认为这里就是内存。既然/dev/shm是内存，那么想当然，我们不能把全部内存都挂载到这个目录下，系统默认只分一半是有道理的。那么我们能不能更改这个tmpfs的大小？ 当然可以！</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># mount -o remount,size=180M tmpfs /dev/shm</span></div><div class="line">[root@localhost ~]<span class="comment"># df -h</span></div><div class="line">Filesystem                    Size  Used Avail Use% Mounted on</div><div class="line">/dev/mapper/VolGroup-lv_root   18G  1.4G   16G   9% /</div><div class="line">tmpfs                         180M     0  180M   0% /dev/shm</div><div class="line">/dev/sda1                     485M   31M  429M   7% /boot</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;所以说，这个tmpfs是可以更改的，但这种办法知识临时的，重启后还会恢复内存大小的一半。那如何让他永久生效？、</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vi /etc/fstab  //编辑/etc/fstab， 把tmpfs这一行改为：</span></div><div class="line">tmpfs                   /dev/shm                tmpfs   defaults,size=180M        0 0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就可以啦。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/19. tmpfs 是什么/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
